[bits 16]
[org 0]
 ; STAGE 2 is loaded to 0x1000:0000
start:
	mov ax,cs
	mov ds,ax
	mov es,ax
	mov ax,0x7000	
	mov ss,ax	; stack segment initialisation
	mov sp,ss
	jc prompta
	mov si, welcome ; print welcome message
	call print

prompta:
	clc
prompt:
	call newline
	mov si, promt
	call print
	xor ECX, ECX 
readloop: ; ECX counts input
	mov ah, 0 ; Wait for keypress
	int 16h
	cmp al, 0xD ; is it ENTER?
	je process ; if so, process it
	mov ah, 0x0E ; else print it
	int 10h
	mov [0x1301 + ECX], al ; and save it
	inc ECX ; also increment char number
	jmp readloop ; start again
process:
	mov [0x1300], CL ; mov the string length to RAM
	cmp cl, 0 ; if length is zero
	je prompt ; jump to prompt
	; first try to cmp with levex
	inc ECX ; increase string length so it takes length into account
cmd1:
	xor EBX, EBX
	mov si, cmd_levex
	call makestr ; make the string to 0x1100
	mov byte [0x999], 0x01 ; we made the check for cmd0x01
	jmp cmdend
cmd2:
	xor EBX, EBX
	mov si, cmd_help
	call makestr
	mov byte [0x999], 0x02
	;inc ECX ; increase string length so it takes length into account
	jmp cmdend
cmd3:
	xor EBX, EBX
	mov si, cmd_text
	call makestr
	mov byte [0x999], 0x03
	jmp cmdend
cmdend:
	xor EBX, EBX
	xor EDX, EDX
loopie:
	cmp ECX, EDX
	je processed
	xor EBX, EBX
	mov bl, [0x1300 + EDX] ; byte of input
	mov bh, [0x1200 + EDX] ; byte of excepted
	inc EDX
	cmp bl, bh
	jne mismatch
	jmp loopie
mismatch:
	cmp byte [0x999], 0x01
	je cmd2
	cmp byte [0x999], 0x02
	je cmd3
uncmd:
	call newline
	mov si, ukcmd
	call print
	jmp prompt
processed:
	xor EDX, EDX
	xor ECX, ECX
	xor EBX, EBX
	cmp byte [0x999], 0x01
	je clevex
	cmp byte [0x999], 0x02
	je chelp
	cmp byte [0x999], 0x03
	je ctext
	
	jmp uncmd
clevex:
	call newline
	mov si, cmd_levex_msg
	call print
	call newline
	mov ax, 0x2000
	mov es, ax
	mov al, 1 ; number of sectors
	mov bx,0
	mov dl, 80h ; drive
	mov dh,0 ; head
	mov ch,0 ; track
	mov cl, 3 ; sector
	mov ah, 2 ; fREAD
	int 0x13
	jmp 0x2000:0000
chelp:
	call newline
	mov si, cmd_help_msg
	call print
	jmp prompt
ctext:
	mov byte [0x997], 0x3E
	stc
	jmp 0x2000:0000
	
hang:
	call hang
	
; =========================== FUNCTIONS ===========================;

; PRINT STRING
print:
	mov bp,sp
	cont:
		lodsb
		or al,al
		jz dne
		mov ah,0x0e
		;mov bx,0
		int 10h
		jmp cont
dne:
	mov sp,bp
	ret
	
; ==============

; PRINT NEWLINE
newline:
        mov si, newlinechar
        call print
        retn
; =============

; Makes string from DB
makestr:
	lodsb           ; AL = [DS:SI]
	or al, al       ; Set zero flag if al=0
	jz mne     ; jump to putstrd if zero flag is set
	mov [0x1201 + EBX], al
	inc EBX
	jmp makestr
mne:
	;mov sp,bp
	mov byte [0x1200], BL
	;pop ECX
	ret
	
; ========================= DATA ================================;
newlinechar db 0Dh, 0Ah, 0
welcome db 'Welcome to LevOS16RW!',10,13,0
ukcmd db 'Unknown command, use help',0
testie db 'Test',0
cmd_levex_msg db 'Loading LevFS...',0
cmd_levex db 'levex',0
cmd_help db 'help', 0
cmd_help_msg db 'Commands: help;levex;text',0
cmd_text db 'text',0
promt db '[LevOS]$ ',0
times 512 - ($-$$) db 0